package com.haois.mp.wechat.handler;

import com.haois.mp.database.module.common.daomain.Parameter;
import com.haois.mp.database.module.wxserver.service.EventHandlerService;
import com.haois.mp.wechat.constants.Enum.MenuEnum;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * Created by RuiHaoZ on 2017/12/8.
 */
@Component
public class MoreInfoHandler extends AbstractHandler implements WxMpMessageHandler {

    @Autowired
    private EventHandlerService eventHandlerService;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMpXmlMessage, Map<String, Object> map, WxMpService wxMpService, WxSessionManager wxSessionManager) throws WxErrorException {
        String content = this.execute(wxMpXmlMessage, wxMpService);
        return WxMpXmlOutMessage.TEXT()
                .fromUser(wxMpXmlMessage.getToUser())
                .toUser(wxMpXmlMessage.getFromUser())
                .content(content).build();
    }

    @Override
    public String execute(WxMpXmlMessage wxMpXmlMessage, WxMpService wxMpService) {
        String content = "";
        content += "更多服务，请回复序号:" + "\n";
        List<Parameter> param = eventHandlerService.getParameter(MenuEnum.BTN_INFO_MORE.getKey());
        for (int i = 0; i < param.size(); i++) {
            content += "\n" + "[" + param.get(i).getNumber() + "]" + param.get(i).getContent();
        }
        eventHandlerService.freshenEvent(wxMpXmlMessage);
        return content;
    }

}
